package com.bilibili.juc.lockSupport;

import java.lang.reflect.Type;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zuife
 * @description
 * @since 2024-04-29 11:41:23
 */
public class LockSupportDemo1 {
    Object objectLock = new Object();

    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        /*
         * 1.synchronized wait/notify
         * 2.lock condition.await/signal
         * 3.LockSupport park/unpark
         */
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println(Thread.currentThread().getName() + "\t ---------come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t ---------被唤醒");
            } catch (InterruptedException e) {
                lock.unlock();
            }
        }, "t1").start();
        //停一秒
        try {
            TimeUnit.SECONDS.sleep(1);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
            }catch (Exception e){
                lock.unlock();
            }
        }, "t2").start();
    }
}
